﻿using System;
using System.Data;
using System.Data.SqlClient;


public   class RegisterDAL
    {
      dbConnection db = new dbConnection();
      public  int CreateAccount(RegisterBO Register)
      {
          SqlConnection cn = null;
          SqlCommand cmd = null;
          int Counter = 0;



          try
          {
              cn = new SqlConnection();
              cn.ConnectionString = db.GetCS();

              cmd = new SqlCommand();
              cmd.Connection = cn;
              cmd.CommandType = CommandType.Text;
              cmd.CommandText = "INSERT INTO [Registrations] ( AccountId, UserName,Password) VALUES ( @AccountId, @UserName, @Password)";
              //CONVERT(VARCHAR(20),@AccCreatedDate, 101)
              //cmd.Parameters.AddWithValue("@CategoryId", CategoryId);
              //cmd.Parameters.AddWithValue("@AccountId", Account.AccountId);
              cmd.Parameters.AddWithValue("@AccountId", Register.AccountId);
              cmd.Parameters.AddWithValue("@UserName", Register.UserName);
              cmd.Parameters.AddWithValue("@Password", Register.Password);
              //cmd.Parameters.AddWithValue("@CreatedDate", Register.CreatedDate);


              cn.Open();
              Counter = cmd.ExecuteNonQuery();

          }
          catch (SqlException SQlex)
          {
             
              if (SQlex.Message.Contains("UNIQUE KEY constraint "))
                  throw new Exception("An account with the Account Id you entered already exsists");

              if (SQlex.Message.Contains("PRIMARY KEY constraint"))
                  throw new Exception("An account with the UserName you entered already exsists try another UserName");
          }
          catch (Exception ex)
          {
              throw new Exception(ex.Message);
          }
          finally
          {
              if (cn != null)
              {
                  cn.Close();
                  cn.Dispose();
                  cn = null;
              }
          }
          return (Counter);
      }

      public  DataTable AccountFillDropDown()
      {
          SqlConnection cn = null;
          SqlCommand cmd = null;
          DataSet dSet1 = null;
          SqlDataAdapter sda = null;

          try
          {
              cn = new SqlConnection();
              cn.ConnectionString = db.GetCS();
              cmd = new SqlCommand();
              cmd.Connection = cn;
              cmd.CommandType = CommandType.Text;
              cmd.CommandText = "Select AccountId from [Accounts]";
              dSet1 = new DataSet();
              sda = new SqlDataAdapter(cmd);

              sda.Fill(dSet1, "Accounts");

              return dSet1.Tables["Accounts"];

          }

          catch
          {

              throw;

          }

          finally
          {

              dSet1.Dispose();

              sda.Dispose();

              cn.Close();

              cn.Dispose();

          }
      }

    }

